package swd.work.generator.v2.java.bean;

import java.io.*;
import java.util.*;

import com.pub.database.*;

import swd.work.generator.v2.java.base.*;

public class FlowBean extends BeanExtend implements BeanImplement {
    private String gstrFlowCode = null;


    public FlowBean(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(pqbnDAO, pstrUserNO);
        gstrFlowCode = gstrSysNO.toUpperCase();
    }

    /**
     * outputApproveActionAndCheck
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputApproveActionAndCheck(BufferedWriter pbwrPrgNO) {
        Hashtable<String,Hashtable<String,Object>> lhtFlowNO = null;
        Hashtable<String,Object> lhtFlowNOData = null;
        int lintFlowNO_cnt = 0;

        try {
            lhtFlowNO = gqbnDAO.hashtableSQL(
                    "select flow_no from floweng_setup where flow_code = '" +
                    gstrFlowCode + "'");

            while (lhtFlowNO != null &&
                   lhtFlowNO.get(String.valueOf(lintFlowNO_cnt)) != null) {

                lhtFlowNOData = (Hashtable<String,Object>) lhtFlowNO.get(String.valueOf(
                        lintFlowNO_cnt));

                lintFlowNO_cnt++;

                outputPreAppFlow(pbwrPrgNO,
                                 lhtFlowNOData.get("FLOW_NO").toString());
                outputAppActionFlow(pbwrPrgNO,
                                    lhtFlowNOData.get("FLOW_NO").toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lhtFlowNOData != null) {
                lhtFlowNOData.clear();
            }
            lhtFlowNOData = null;
            if (lhtFlowNO != null) {
                lhtFlowNO.clear();
            }
            lhtFlowNO = null;
        }
    }

    /**
     * outputCheckAddRoleMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputAddActionMethod(BufferedWriter pbwrPrgNO) throws
            Exception {

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public String addAction(View view){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String lstrNotNullList = null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String lstrAddStep = null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String lstrInsResult=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try {", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrAddStep=\"check_not_null_fields\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrNotNullList = business.checkNotNullFld(view);",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "if (lstrNotNullList.equals(\"\")) {", 2);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrAddStep=\"save_step\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrInsResult=business.exeInsSQL(view);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "if(lstrInsResult.equals(\"\")){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrAddStep=\"flow_send_step\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "(new FlowExpand(\"O\",view.getDAO().getConnection())).expandFlow(view.getFlow_code(),view.getFlow_num(),gstrFlowMasterTable,view.getEmpno());",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}else{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             " return \"save_data_error\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "} else {", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return lstrNotNullList + \" can_not_null\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "view.setPrgStatus(\"null\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return \"flow_send_success\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "} catch (Exception e) {", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "e.printStackTrace();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return \"prg_run_error\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputCheckAppFlow
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrFlowNO String
     */
    private void outputAppActionFlow(BufferedWriter pbwrPrgNO,
                                     String pstrFlowNO) throws
            Exception {

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String appFlow" + pstrFlowNO +
                             "Action(View view) throws Exception{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "callFlowEngApp(view,\"P\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return \"approval_success\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "throw new Exception(e.toString());", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputCheckRejectRoleMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputRejectActionMethod(BufferedWriter pbwrPrgNO) throws
            Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "void rejectAction(View view) throws Exception{",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "callFlowEngApp(view,\"X\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    private void outputCallFlowEngApp(BufferedWriter pbwrPrgNO) throws
            Exception {

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "void callFlowEngApp(View view,String pstrJstFlag) throws Exception{",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "Hashtable lhtFlowApp= new Hashtable();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"fnum\",view.getFlow_num());", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"semp\",view.getLoginUserNO());",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"seq\",view.getSeqno());", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"table\",gstrFlowMasterTable);",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"jstflag\",pstrJstFlag);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "(new FlowApprove(\"O\",view.getDAO().getConnection(),lhtFlowApp)).flowApp();",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "throw new Exception(e);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}finally{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.clear();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

    }

    /**
     * outputClassVarible
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputClassVarible(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String gstrFlowCode=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String gstrFlowMasterTable=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "Business business=null;", 1);
    }

    /**
     * outputContructor
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputContructor(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public Flow(String pstrFlowCode,String pstrFlowMasterTable,Business pBusiness){",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gstrFlowCode=pstrFlowCode;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gstrFlowMasterTable=pstrFlowMasterTable;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "business=pBusiness;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputPreRejectActionMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputPreRejectActionMethod(BufferedWriter pbwrPrgNO) throws
            Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void preRejectAction(View view){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "e.printStackTrace();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputSetPreAddActionMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputPreAddActionMethod(BufferedWriter pbwrPrgNO) throws
            Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void preAddAction(View view){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "view.setFlow_code(gstrFlowCode);",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "view.setFlow_num(new GetFlowNum(view.getDAO().getDBType(),view.getDAO().getConnection()).getFlowNum(gstrFlowCode));",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "business.setInitValueForAdd(view);",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "e.printStackTrace();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputSetPreAppFlow
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrFlowNO String
     */
    private void outputPreAppFlow(BufferedWriter pbwrPrgNO,
                                  String pstrFlowNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void preAppFlow" + pstrFlowNO + "(){",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "//put approve init code in there", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    public String createBean(String pstrPrgNO) {
        String lstrCreateResult = "";
        String lstrBeanFileName = "Flow.java";
        BufferedWriter lbwrPrgNO = null;

        try {
            super.setBaseVarible(pstrPrgNO);

            lbwrPrgNO = super.getFwrPrgNO(lstrBeanFileName, "jsp_frame_path",
                                          "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                          "\\" + pstrPrgNO + "\\", gstrUserNO);

            super.outputBeanHeader(lbwrPrgNO, super.getSysNO(gstrUserNO),
                                   pstrPrgNO, lstrBeanFileName, gstbTabStr,
                                   gstrTabChar);

            //output class level varible
            outputClassVarible(lbwrPrgNO);

            //output Constructor
            outputContructor(lbwrPrgNO);

            //新增程式
            if (this.gstrPrgKind.equals("D")) {
                //output setPreAddAction method.
                outputPreAddActionMethod(lbwrPrgNO);

                //output checkAddRole method
                outputAddActionMethod(lbwrPrgNO);
            }

            //簽核程式
            if (this.gstrPrgKind.equals("Y")) {
                //output checkRejectRole method
                outputRejectActionMethod(lbwrPrgNO);

                //output checkRejectRole method
                outputPreRejectActionMethod(lbwrPrgNO);

                outputApproveActionAndCheck(lbwrPrgNO);

                outputCallFlowEngApp(lbwrPrgNO);

                ouputReadFlowSeqno(lbwrPrgNO);

                outputCheckJSTEmpno(lbwrPrgNO);
            }
            //output class end single
            super.outputJavaCode(lbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                lbwrPrgNO.flush();
                lbwrPrgNO.close();
                lbwrPrgNO = null;
            } catch (Exception se) {
                se.printStackTrace();
                // throw new Exception("can't flush FlowBean");
            }
        }

        return lstrCreateResult;
    }

    /**
     * outputCheckJSTEmpno
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputCheckJSTEmpno(BufferedWriter pbwrPrgNO) throws Exception{
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                     "boolean checkJSTEmpno(View view) throws Exception{",
                     1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                     "return view.getDAO().readSingleValueData(\"select count(r_empno) from wkf_jst_eng where flow_num='\" + view.getFlow_num() + \"' and seqno='\" + view.getSeqno() + \"'\").equals(view.getLoginUserNO());",
                     1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                     "}",
                     1);
    }

    /**
     * ouputReadFlowSeqno
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void ouputReadFlowSeqno(BufferedWriter pbwrPrgNO) throws Exception{
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String readFlowSeqno(View view,String pstrJstFlag) throws Exception{",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return view.getDAO().readSingleValueData(\"select seqno from wkf_jst_eng where flow_num='\" + view.getFlow_num() + \"' and r_empno='\" + view.getLoginUserNO() + \"' and jst_flag='\" + pstrJstFlag + \"'\");",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}",
                             1);
    }


}
